using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.ServiceModel.Syndication;

using System.Xml;
 

namespace ShopzillaV1.Controllers
{
    public class FeedController : ApplicationController
    {
        public ActionResult TopSellers(string id)
        {
            string[] idItems = id.Split(new char[] { '-' });
            string searchIndex = idItems[1];
            string nodeid = idItems[0];
         
            string currentPortalUrl = HttpContext.Request.Url.Authority;
            currentPortalUrl = "http://" + currentPortalUrl;
            SyndicationFeed feed =
       new SyndicationFeed("Top Sellers",
                           "Top Sellers for Browse Node " + nodeid,
                           new Uri(currentPortalUrl + "/Feed/TopSellers/" + id),
                           "TopSellers" + id,
                           new DateTimeOffset(DateTime.Now));
            List<SyndicationItem> items = new List<SyndicationItem>();
            return new RssActionResult() { Feed = feed };


        }


        public ActionResult Categories()
        {
            string currentPortalUrl = HttpContext.Request.Url.Authority;
            currentPortalUrl = "http://" + currentPortalUrl;
            SyndicationFeed feed =
       new SyndicationFeed("All Categories",
                           "This feed includes all categories",
                           new Uri(currentPortalUrl + "/Feed/Categories"),
                           "Categories",
                           new DateTime(2009, 10, 1));

           
            return new RssActionResult() { Feed = feed };
        }

        public class RssActionResult : ActionResult
        {
            public SyndicationFeed Feed { get; set; }

            public override void ExecuteResult(ControllerContext context)
            {
                context.HttpContext.Response.ContentType = "application/rss+xml";

                Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
                using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
                {
                    rssFormatter.WriteTo(writer);
                }
            }
        }

    }
}
